package org.twndmv.dto;

import org.twndmv.bo.Person;
import org.twndmv.bo.PhysicalDetails;
import org.twndmv.bo.Residence;
import org.twndmv.bo.VehicleRegistration;



public class DTOUtils {

	
	public static Person convertDTOToPerson(PersonDTO oldPeep) {
		Person result = new Person();
		result.setFirstName(oldPeep.getFirstName());
		result.setLastName(oldPeep.getLastName());
		result.setCurrentResidence(DTOUtils.convertDTOToResidence(oldPeep.getCurrentResidence()));
		result.setNameSuffix(oldPeep.getNameSuffix());
		result.setMiddleName(oldPeep.getMiddleName());
		
		for(ResidenceDTO res : oldPeep.getResidences()) {
			result.getResidences().add(DTOUtils.convertDTOToResidence(res));
		}
		
		/*for(VehicleRegistrationDTO veh : oldPeep.getRegistrations()) {
			result.getRegistrations().add(DTOUtils.convertDTOToVehReg(veh));
		}*/
		
		result.setPhysicalDetails(DTOUtils.convertDTOToPhysicalDetails(oldPeep.getPhysicalDetails()));
		return result; 
	}
	
	public static PhysicalDetails convertDTOToPhysicalDetails(
			PhysicalDetailsDTO physicalDetails) {
		if(physicalDetails != null) {
		PhysicalDetails result = new PhysicalDetails();
		result.setDob(physicalDetails.getDob());
		result.setEyeColor(physicalDetails.getEyeColor());
		result.setHairColor(physicalDetails.getHairColor());
		result.setHeight(physicalDetails.getHeight());
		result.setWeight(physicalDetails.getWeight());
		result.setSEX(physicalDetails.getSEX());
		return result;
		} else {
        	return null;
        }
	}
	
	private static PhysicalDetailsDTO convertPhysicalDetailsToDTO(
			PhysicalDetails physicalDetails) {
		if(physicalDetails != null) {
		PhysicalDetailsDTO result = new PhysicalDetailsDTO();
		result.setDob(physicalDetails.getDob());
		result.setEyeColor(physicalDetails.getEyeColor());
		result.setHairColor(physicalDetails.getHairColor());
		result.setHeight(physicalDetails.getHeight());
		result.setWeight(physicalDetails.getWeight());
		result.setSEX(physicalDetails.getSEX());
		
		return result;
        } else {
        	return null;
        }
	}

	public static Residence convertDTOToResidence(ResidenceDTO oldRes) {
		
		if(oldRes != null) {
		Residence result = new Residence();
		
		result.setStreetNo(oldRes.getStreetNo());
		result.setStreetName(oldRes.getStreetName());
		result.setCity(oldRes.getCity());
		result.setState(oldRes.getState());
		result.setZipCode(oldRes.getZipCode());
		result.setMoveIn(oldRes.getMoveIn());
		result.setMoveOut(oldRes.getMoveOut());
		
		return result;
		} else {
			return null;
		}
	}
	
	public static ResidenceDTO convertResidenceToDTO(Residence oldRes) {
		
		if(oldRes != null) {
		ResidenceDTO result = new ResidenceDTO();
		
		result.setStreetNo(oldRes.getStreetNo());
		result.setStreetName(oldRes.getStreetName());
		result.setCity(oldRes.getCity());
		result.setState(oldRes.getState());
		result.setZipCode(oldRes.getZipCode());
		result.setMoveIn(oldRes.getMoveIn());
		result.setMoveOut(oldRes.getMoveOut());
		
		return result;
		} else {
			return null;
		}
	}
	
	public static PersonDTO convertPersonToDTO(Person oldPeep) {
		PersonDTO result = new PersonDTO(oldPeep.getId());
		result.setFirstName(oldPeep.getFirstName());
		result.setLastName(oldPeep.getLastName());
		result.setCurrentResidence(DTOUtils.convertResidenceToDTO(oldPeep.getCurrentResidence()));
		result.setNameSuffix(oldPeep.getNameSuffix());
		result.setMiddleName(oldPeep.getMiddleName());
		
		for(Residence res : oldPeep.getResidences()) {
			result.getResidences().add(DTOUtils.convertResidenceToDTO(res));
		}
		
		/*for(VehicleRegistration veh : oldPeep.getRegistrations()) {
			result.getRegistrations().add(DTOUtils.convertVehRegToDTO(veh));
		}*/
		
		result.setPhysicalDetails(DTOUtils.convertPhysicalDetailsToDTO(oldPeep.getPhysicalDetails()));
		
		
		return result;
	}
	
	public static VehicleRegistrationDTO convertVehRegToDTO(VehicleRegistration newVR) {
		VehicleRegistrationDTO result = new VehicleRegistrationDTO();
		
		result.setId(newVR.getId());
		result.setColor(newVR.getColor());
		result.setExpiration(newVR.getExpiration());
		result.setMake(newVR.getMake());
		result.setModel(newVR.getModel());
		result.setTagNo(newVR.getTagNo());
		result.setVin(newVR.getVin());
		result.setYear(newVR.getYear());
		
		for(Person person : newVR.getOwners()) {
		  result.getOwners().add(DTOUtils.convertPersonToDTO(person));
		}
		
		return result;
	}
	
	public static VehicleRegistration convertDTOToVehReg(VehicleRegistrationDTO newVR) {
		VehicleRegistration result = new VehicleRegistration();
		
		
		result.setColor(newVR.getColor());
		result.setExpiration(newVR.getExpiration());
		result.setMake(newVR.getMake());
		result.setModel(newVR.getModel());
		result.setTagNo(newVR.getTagNo());
		result.setVin(newVR.getVin());
		result.setYear(newVR.getYear());
		
		for(PersonDTO person : newVR.getOwners()) {
			result.getOwners().add(DTOUtils.convertDTOToPerson(person));
			}
		
		return result;
	}
}
